#include<bits/stdc++.h>
using namespace std;


unordered_map<string, string> mp;


string find(string x) {
	if (mp[x] != x)	mp[x] = find(mp[x]);
	return mp[x];
}


void merge(string x, string y) {	// father - son
	mp[y] = x;
}


signed main() {
	char ch;
	string s;

	string father;

	while (cin >> ch) {
		if (ch == '$')	break;

		cin >> s;

		if (ch == '#') {
			father = s;
			if (mp.count(s) == 0)
				mp[s] = s;
		} else if (ch == '+') {
			merge(father, s);
		} else
			cout << s << " " << find(s) << "\n";

	}
	return 0;
}
